package com.amor.ui

import android.content.Context
import android.content.SharedPreferences
import android.view.View
import com.amor.app.BaseActivity
import com.amor.demo.web1.BR
import com.amor.demo.web1.R
import com.amor.demo.web1.databinding.VProxyBinding
import com.amor.vm.ProxyViewModel
import com.hy.frame.ui.ToolbarUI

/**
 * title 代理配置页
 * author hz21097252
 * date 2022/3/3
 * desc 无
 */
class ProxyActivity : BaseActivity<VProxyBinding, ProxyViewModel>() {
    private var toolbar: ToolbarUI? = null
    private var shared: SharedPreferences? = null
    override val layoutId: Int = R.layout.v_proxy

    override val viewModel: Class<ProxyViewModel> = ProxyViewModel::class.java

    override val variableId: Int = BR.model

    override fun initView() {
        this.toolbar = com.amor.widget.ToolbarUI(context).apply {
            setTitle("代理配置")
            setTranslucentStatus(true)
            build(mBinding?.layout)
        }
    }

    override fun initData() {
        shared = getSharedPreferences("DATA_PROXY", Context.MODE_PRIVATE)
        mBinding?.editHost?.setText(getHistoryHost())
        mBinding?.editPort?.setText(getHistoryPort())
    }

    private fun getHistoryHost(): String {
        return shared?.getString("data_host", null) ?: "192.168.98.56"
    }

    private fun getHistoryPort(): String {
        return shared?.getString("data_port", null) ?: "8899"
    }

    private fun setHistoryHost(host: String?) {
          shared?.edit()?.putString("data_host", host)?.apply()
    }

    private fun setHistoryPort(port: String?) {
        shared?.edit()?.putString("data_port", port)?.apply()
    }

    override fun initObserver() {
        super.initObserver()
        mModel?.click?.value = View.OnClickListener {
            when (it.id) {
                R.id.btn_jump -> {
                    val host = mBinding?.editHost?.text?.toString()
                    val port = mBinding?.editPort?.text?.toString()
                    setHistoryHost(host)
                    setHistoryPort(port)
                    ProxyWebActivity.startAct(context, host, port)
                }
            }
        }
    }
}